<template>
	<view>
		<!-- 顶部搜索框 -->
		<view class="search-box">
			<uni-search-bar :radius="100" @input="input" :value="kw" cancelButton="none" :focus="true"></uni-search-bar>
		</view>

		<!-- 搜索建议列表-->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item)">
				<view class="goods-name">{{ item.goods_name }}</view>
				<uni-icons type="forward" size="16"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 列表 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, index) in historyList" :key="index"
					@click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器
				timer: null,
				// 搜索结果列表
				searchResults: [],
				// 搜索历史记录
				historyList: [],
				// 关键字
				kw: ''
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// input输入时间
			input(e) {
				// 清除timer对应的延时器
				clearTimeout(this.timer)
				// 重新启动延时器
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			// 获取搜索结果列表
			async getSearchList() {
				// 判断关键字是否为空
				if (this.kw.length === 0) {
					this.searchResults = []
					return
				}

				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
			},
			// 跳转到商品详情页
			gotoDetail(item) {
				// 将搜索项添加入搜索历史记录
				this.addSearch()

				// 路由跳转
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id,
				})
			},
			// 将搜索项添加入搜索历史记录
			addSearch() {
				// 判断搜索历史记录中是否存在相同项
				if (this.historyList.indexOf(this.kw) !== -1) {
					this.historyList.splice(this.historyList.indexOf(this.kw), 1)
				}
				this.historyList.unshift(this.kw)
				// 将搜索历史记录进行持久化存储
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			// 清空历史记录
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			// 搜索列表项被点击
			gotoGoodsList(item) {
				this.kw = item
				this.addSearch()
				
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
				this.kw = ''
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
		background-color: #C00000;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 5px;

		.history-title {
			height: 40px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>